package com.xd.dubbo.globalTransaction.aspect;

import com.xd.dubbo.globalTransaction.connection.GlobalConnection;
import com.xd.dubbo.globalTransaction.transactional.GlobalTransactionalManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.sql.Connection;

@Aspect
@Component
public class GlobalDataSourceAspect {

    @Around("execution(* javax.sql.DataSource.getConnection(..))")
    public Connection around(ProceedingJoinPoint point) throws Throwable {
        if (GlobalTransactionalManager.getCurrent() != null) {
            return new GlobalConnection((Connection) point.proceed(), GlobalTransactionalManager.getCurrent());
        } else {
            return (Connection) point.proceed();
        }
    }
}
